﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using AppEnvironment.Lib;
using System.Collections.Specialized;

namespace AppEnvironment.Web
{
    public class Navigator
    {
        //the following routine uses Response.Redirect
        //to ensure this works during an Ajax callback, use the following web.config entry:

        //<httpModules>
        // <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        //</httpModules>

        public virtual void GoTo(object view, object navParams)
        {
            string pageUrl = GetUrl(view, navParams);
            HttpContext.Current.Response.Redirect(pageUrl);
        }

        
        public virtual TParamType MakeNavParams<TParamType>()
        {
            TParamType navParams = App.New<TParamType>();
            NavParam paramsObj = navParams as NavParam;
            if (paramsObj != null)
            {
                paramsObj.ListToParams(GetQueryCollection<TParamType>());
            }
            return navParams;
        }

        protected virtual NameValueCollection GetQueryCollection<TParamType>()
        {
            return HttpContext.Current.Request.QueryString;
        }

        private string GetUrl(object view, object navParams)
        {
            return GetUrl(view, navParams, false);
        }

        public virtual string GetUrl(object view, object navParams, bool isReturn)
        {
            string pageUrl = (string)view;
            NavParam paramsObj = navParams as NavParam;
            if (paramsObj != null)
            {
                NameValueCollection query = new NameValueCollection();
                paramsObj.ParamsToList(query);
                StringBuilder builder = new StringBuilder();
                builder.Append(pageUrl);

                bool firstSeperatorNeeded = !pageUrl.Contains('?');
                foreach (string key in query.Keys)
                {
                    if (firstSeperatorNeeded)
                    {
                        builder.Append("?");
                        firstSeperatorNeeded = false;
                    }
                    else
                    {
                        builder.Append("&");
                    }
                    builder.Append(key).Append("=");
                    builder.Append(HttpUtility.UrlEncode(query[key]));
                }

                if (isReturn)
                {
                    if (firstSeperatorNeeded)
                    {
                        builder.Append("?");
                    }
                    else
                    {
                        builder.Append("&");
                    }
                    builder.Append("nvrt=y");
                }

                pageUrl = builder.ToString();
            }
            return pageUrl;
        }

        public virtual bool IsReturning()
        {
            return HttpContext.Current.Request.QueryString["nvrt"] == "y";
        }

    }
}
